#include "SSplitter.h"
#include <core/StyleMgr.h>
#include <QStylePainter>
#include <QSplitterHandle>
#include <QEvent>

class SpliterHandler : public QSplitterHandle
{
public:
	SpliterHandler(Qt::Orientation o, QSplitter* parent)
		: QSplitterHandle(o, parent)
	{
		setAttribute(Qt::WA_Hover);
	}
protected:
	bool event(QEvent* event) override
	{
		switch (event->type()) {
		case QEvent::HoverEnter:
			m_hover = true;
			update();
			break;
		case QEvent::HoverLeave:
			m_hover = false;
			update();
			break;
		default:
			break;
		}
		return QSplitterHandle::event(event);
	}
	void paintEvent(QPaintEvent* e) override
	{
		//TODO:纵向
		QSplitterHandle::paintEvent(e);
		QStylePainter painter(this);

		QRect r = contentsRect();
		int offset = r.width() / 2 - 24;
		r.adjust(offset, 1, -offset, -1);

		painter.setPen(Qt::NoPen);
		painter.setBrush(StyleMgr::instance().color(m_hover ? "Hover" : "BorderNormal"));
		painter.drawRect(r);
	}
private:
	bool	m_hover = false;
};

SSplitter::SSplitter(Qt::Orientation orientation, QWidget* parent)
	: QSplitter(orientation, parent)
{
	setStyleSheet(QString("SSplitter {background-color:#%1}").arg(StyleMgr::instance().color("BasicBackground").rgb(), 0, 16));
}

QSplitterHandle* SSplitter::createHandle()
{
	return new SpliterHandler(orientation(), this);
}

